package edu.hawaii.ics414;

import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
  /** Called when the activity is first created. */
  private ListView list;
  private JSONArray jArray;
  private String[] sArray;
  private final String url = "http://islandtabs.dyndns.org/fetch.php";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    StrictMode.enableDefaults();
    list = (ListView) findViewById(R.id.listView1);
    DataRequester dr = new DataRequester();
    displayEvents(dr.getData(url));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    switch (item.getItemId()) {
    case R.id.home:
      Intent main = new Intent(getApplicationContext(), MainActivity.class);
      main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(main);
      break;

    case R.id.calendar:
      Intent calendar = new Intent(getApplicationContext(), CalendarActivity.class);
      calendar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(calendar);
      break;

    /*case R.id.nearbyevents:
      Intent nearbyEvents = new Intent(getApplicationContext(), NearbyEventsActivity.class);
      nearbyEvents.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(nearbyEvents);
      break;

    case R.id.info:
      Intent info = new Intent(getApplicationContext(), InfoActivity.class);
      info.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(info);
      break;*/

    }
    return true;
  }

  private void displayEvents(String events) {
    try {
      jArray = new JSONArray(events);
      sArray = new String[jArray.length()];

      for (int i = 0; i < jArray.length(); i++) {
        sArray[i] = jArray.getJSONObject(i).getString("title");
      }
      ArrayAdapter<String> adapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sArray);
      list.setAdapter(adapter);
      list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          Intent intent = new Intent("edu.hawaii.ics414.DETAILS");
          Bundle b = new Bundle();
          try {
            b.putString("title", jArray.getJSONObject(position).getString("title"));
            b.putString("description", jArray.getJSONObject(position).getString("description"));
            b.putString("date", jArray.getJSONObject(position).getString("date"));
            b.putString("venue", jArray.getJSONObject(position).getString("venue"));
          }
          catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
          }
          intent.putExtras(b);
          startActivity(intent);
        }
      });

    }
    catch (Exception e) {
      Log.e("log_tag", "Error parsing data " + e.toString());
    }
  }
}